<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>添加水费记录-水费交款</title>
  </head>
  <body>
    <script type="text/javascript">
      Ext.onReady(function(){
        Ext.QuickTips.init();
        Ext.BLANK_IMAGE_URL = '/javascripts/ext/resources/images/default/s.gif';
        //设置对话框最大宽度
        Ext.Msg.getDialog().setWidth(300);
        var houseId = new Ext.form.Hidden({
          name: 'house_id',
          value: '<%=@house.id%>'
        });

        var ownerNumber = new Ext.form.TextField({
          fieldLabel: '业主编号',
          allowBlank: false,
          name: 'number',
          anchor: '90%',
          readOnly:true,
          value:'<%=@house.number%>'
        });
        var owner = new Ext.form.TextField({
          fieldLabel: '业主姓名',
          allowBlank: false,
          name: 'owner',
          anchor: '90%',
          readOnly:true,
          value:'<%=@house.owner%>'
        });
        var waterFeeLastDate = new Ext.form.TextField({
          fieldLabel: '交费结止',
          allowBlank: false,
          name: 'owner',
          anchor: '90%',
          readOnly:true,
          value:'<%=@house.water_pay_year%>年<%=@house.water_pay_month%>月'
        });

        var waterBalance =  new Ext.form.TextField({
          fieldLabel: '上次余额',
          allowBlank: false,
          name: 'balance',
          anchor: '90%',
          readOnly:true,
          value:'<%=@house.balance%>'
        });

        //上次抄表数
        var lastWaterMeter =  new Ext.form.NumberField({
          fieldLabel: '上次抄表数',
          allowBlank: false,
          name: 'last_meter',
          readOnly: true,
          anchor: '90%',
          value:'<%=@house.water_last_meter%>'
        });

        <%if @waterMeter != nil%>
            var lastWaterRecordYear = <%=@waterMeter.current_year %>;
            var lastWaterRecordMonth = <%=@waterMeter.current_month %>;
            var currentMeter = <%=@waterMeter.current_meter%>;
        <%else%>
            var lastWaterRecordYear = 0;
            var lastWaterRecordMonth = 0;
            var currentMeter = 0;
        <%end%>

        //本次抄表数
        var currentWaterMeter =  new Ext.form.NumberField({
          fieldLabel: '本次抄表数',
          allowBlank: false,
          name: 'current_meter',
          allowDecimals: false,
          allowNegative: false,
          enableKeyEvents:true,
          anchor: '90%',
          value: currentMeter
        });

        //本次用水量
        var waterQuantity =  new Ext.form.NumberField({
          fieldLabel: '本次用水量',
          allowBlank: false,
          name: 'water_quantity',
          readOnly: true,
          anchor: '90%'
        });
        //本次金额
        var waterAmount =  new Ext.form.NumberField({
          fieldLabel: '本次金额',
          allowBlank: false,
          name: 'amount',
          anchor: '90%',
          readOnly:true
        });        
        
        currentWaterMeter.on('keyup',countWaterFee);
        //输入非常快时，keyup也不安全。
        currentWaterMeter.on('blur',countWaterFee);
        //抄表时间
        var recordingDate =  new Ext.form.DateField({
          fieldLabel: '抄表日期',
          name: 'meter_recording_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-18",
          allowBlank: true,
          blankText: "抄表日期必须选择",
          emptyText: '请选择日期...',
          anchor: '90%'
        });       
        
        var waterCurrentYear =  new Ext.form.NumberField({
          fieldLabel: '本次交费年',
          allowBlank: false,
          name: 'current_year',
          allowDecimals: false,
          allowNegative: false,
          enableKeyEvents:true,
          anchor: '90%',
          value: lastWaterRecordYear
        });
        
        var waterCurrentMonth =  new Ext.form.NumberField({
          fieldLabel: '月',
          allowBlank: false,
          name: 'current_month',
          allowDecimals: false,
          allowNegative: false,
          enableKeyEvents:true,
          anchor: '90%',
          value: lastWaterRecordMonth
        });
        
        //本次单价
        var waterUnitPrice =  new Ext.form.NumberField({
          fieldLabel: '本次单价',
          allowBlank: false,
          name: 'unit_price',
          anchor: '90%',
          value: <%=session[:water_unit_price]%>
        });

        //付款方式
        var paymentMethod = new Ext.form.RadioGroup({
          //width : 220,
          fieldLabel: '付款方式',
          //hideLabel:true,
          //style:'margin-left:100px;margin-top:20px',
          name:'payment_method',
          columns : 3,
          anchor: '90%',
          vertical :false,
          items:[
            {boxLabel:'余额',inputValue:'余额',name:'payment_method',checked:true},
            {boxLabel:'现金',inputValue:'现金',name:'payment_method'},
            {boxLabel:'混合',inputValue:'混合',name:'payment_method'}
          ]
        });
        //提交按钮
        var addWaterFeeButton = new Ext.Button({
          text: '提交',
          iconCls: 'icon-save',
          handler: function(){
            addWaterFeeButton.setDisabled(true);
            if (newWaterFeeRecordFormPanel.form.isValid()) {
              //判断一下抄表数是不是比上一次表数还小。
              if(lastWaterMeter.getValue() >= currentWaterMeter.getValue()){
                Ext.MessageBox.show({
                  title: '提示消息',
                  msg: '抄表数小于上一次表数！有误，请核实',
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.INFO
                });
                addWaterFeeButton.enable();
                return;
              }
              //日期结止是不是比上一次结止还小。
              if(waterCurrentYear.getValue() < <%=@house.water_pay_year%>){
                Ext.MessageBox.show({
                  title: '提示消息',
                  msg: '本次结止年份小于上次结止日期！有误，请核实!',
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.INFO
                });
                addWaterFeeButton.enable();
                return;
              }else if(waterCurrentMonth.getValue() < <%=@house.water_pay_month%>){
                Ext.MessageBox.show({
                  title: '提示消息',
                  msg: '本次结止月份小于上次结止日期！有误，请核实!',
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.INFO
                });
                addWaterFeeButton.enable();
                return;
              }
              //如果是用余额付款，要计算一下余额是否够。
              if(waterAmount.getValue() > waterBalance.getValue()){
                if(paymentMethod.getValue().getGroupValue() ==  "余额"){
                  Ext.MessageBox.show({
                    title: '提示消息',
                    msg: '余额不足,请充值后再交款，或选择其它的支付方式。',
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.INFO
                  });
                  addWaterFeeButton.enable();
                  return;
                }
              }else{
                if(paymentMethod.getValue().getGroupValue() !=  "余额"){
                  Ext.MessageBox.show({
                    title: '提示消息',
                    msg: '您当前的余额大于要支付的金额，请选择其它支付方式。',
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.INFO
                  });
                  paymentMethod.reset();
                  addWaterFeeButton.enable();
                  return;
                }
              }
              /*if(paymentMethod.getValue().getGroupValue() ==  "余额" && waterAmount.getValue() > waterBalance.getValue()){
                Ext.MessageBox.show({
                  title: '提示消息',
                  msg: '余额不足',
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.INFO
                });
                addWaterFeeButton.enable();
                return;
              }else if(paymentMethod.getValue().getGroupValue() ==  "混合" && waterAmount.getValue() < waterBalance.getValue()){
                Ext.MessageBox.show({
                  title: '提示消息',
                  msg: '您当前的余额大于要支付的金额，请选择其它支付方式。',
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.INFO
                });
                paymentMethod.reset();
                addWaterFeeButton.enable();
                return;
              }*/

              newWaterFeeRecordFormPanel.form.submit({
                success: function(result, resp){
                  if (resp.result.info.indexOf("成功") >= 0) {
                    newWaterFeeRecordWin.destroy();
                  } else {
                    Ext.MessageBox.alert('消息', resp.result.info);
                    addWaterFeeButton.enable();
                  }
                },
                failure: function(result, request){
                  addWaterFeeButton.enable();
                  Ext.MessageBox.alert('消息', '服务器的通信失败!');
                }
              });
            }
            else {
              addWaterFeeButton.enable();
              //Ext.MessageBox.alert('输入验证有错', '请输入必输选项');
            }
          }
        })
        var newWaterFeeRecordFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          labelWidth: 65,
          width: '400',
          labelAlign: 'right',
          frame : true,
          url: '/fee/createWaterFeeRecord',
          //defaultType: 'textfield',
          items: [{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [ownerNumber]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [owner]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterFeeLastDate]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterBalance]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [lastWaterMeter]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [currentWaterMeter]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterQuantity]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [recordingDate]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterCurrentYear]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterCurrentMonth]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterUnitPrice]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterAmount]
                }]
            },paymentMethod,houseId],
          buttons: [addWaterFeeButton,{
              text: '关闭',
              iconCls: 'exit',
              handler: function(){
                newWaterFeeRecordWin.destroy();
              }
            }]
        });
        
        function countWaterFee(){
          //alert(waterAmount);
          waterQuantity.setValue(currentWaterMeter.getValue()-lastWaterMeter.getValue());
          waterAmount.setValue(((waterQuantity.getValue()*10)*(waterUnitPrice.getValue()*10))/100);
        };
        countWaterFee();
        //如果已经没有要交费的情况，弹出提示，直接关闭窗口
        if (lastWaterRecordYear == 0){
          newWaterFeeRecordWin.destroy();
          Ext.MessageBox.show({
                  title: '提示消息',
                  msg: '已经记录的水表抄表都已经交费.',
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.INFO
                });
        }else{
          newWaterFeeRecordFormPanel.render('new_water_form');
        }
      })
    </script>
    <div id="new_water_form"></div>
  </body>
</html>
